<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
      <!-- 搜索框 -->
        <el-form-item>
          <el-input
            placeholder="请输入内容"
            prefix-icon="el-icon-search"
            v-model="queryInfo.query">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getAllUser">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addUserVisible=true">添加用户</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格数据 -->
      <el-table
        :data="userList"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column
          type="index">
        </el-table-column>
        <el-table-column
          prop="username"
          label="姓名">
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱">
        </el-table-column>
        <el-table-column
          prop="mobile"
          label="电话">
        </el-table-column><el-table-column
          prop="role_name"
          label="角色">
        </el-table-column><el-table-column
          prop="mg_state"
          label="状态">
          <!--插槽 父传子   子组件要使用父组件里面的值-->
          <template v-slot="mySlot">
            <el-switch
              v-model="mySlot.row.mg_state"
              @change="userStatusChange(mySlot.row)"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="mySlot">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
               <el-button @click="editDialog(mySlot.row.id)" type="primary" icon="el-icon-edit" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top">
              <el-button @click="delDialog(mySlot.row)" type="danger" icon="el-icon-delete" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
              <el-button @click="role(mySlot.row.id)" type="warning" icon="el-icon-setting" circle></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

        <!--分页操作-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>


      <!--添加用户的弹出框-->
      <el-dialog title="添加用户" v-model="addUserVisible" width="50%">
                                          <!--ref 类似于dom操作获取节点，这里获取此表单-->
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm" label-width="70px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email" ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="ruleForm.mobile" ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="resetForm('ruleForm')">取 消</el-button>
            <el-button type="primary" @click="addUser('ruleForm')">确 定</el-button>
          </span>
        </template>
      </el-dialog>

    <!-- 编辑用户操作 -->
      <el-dialog
        title="提示"
        v-model="editUserVisible"
        width="30%"
        center>
        <el-form :model="updateForm" status-icon :rules="rules" ref="updateForm" class="demo-ruleForm" label-width="70px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="updateForm.username"  disabled></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="updateForm.email" ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="updateForm.mobile" ></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="editUserVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit">确 定</el-button>
          </span>
        </template>
      </el-dialog>

      <!-- 删除用户操作 -->
      <el-dialog
        title="提示"
        v-model="delUserVisible"
        width="30%"
        center>
        <span>你确定要删除{{userName}}用户吗?</span>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="delUserVisible = false">取 消</el-button>
            <el-button type="primary" @click="del">确 定</el-button>
          </span>
        </template>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
name: "users",
data(){
  return{
    currentPage4:4,
    userList: [],
    queryInfo:{
      query:'',
      pagenum:1, //当前第一页，每一页5条数据
      pagesize:5
    },
    total:0,
    addUserVisible:false,
    delUserVisible: false,
    editUserVisible:false,
    ruleForm:{
      username:'',
      password:'',
      email:'',
      mobile:''
    },
    updateForm:{

    },
    rules:{
      username: [
        { required: true, message:'请输入账号', trigger: 'blur' }
      ],
      password: [
        { required: true, message:'请输入密码', trigger: 'blur' }
      ],
      email: [
        { required: true, message:'请输入邮箱', trigger: 'blur' }
      ],
      mobile: [
        { required: true, message:'请输入手机号', trigger: 'blur' }
      ]
    },
    userId: 0,
    userName: ''
  }
},
  created() {
    this.getAllUser();
  },
  methods:{
    //获取所有的用户数据
    getAllUser(){
      this.axios.get('users',{
        params:this.queryInfo
      }).then(data=>{
         console.log(data);
        if(data.data.meta.status !== 200) return ElMessage.error(data.data.meta.msg);
        //有数据了
        this.userList = data.data.data.users;
        this.total = data.data.data.total;
      }).catch(err=>{
        console.log(err);
      })
    },
    //编辑用户
    editDialog(id){
      this.userId = id;
      this.editUserVisible = true;

      this.axios.get(`users/${this.userId}`).then(resp=>{
        //console.log(resp.data.data);
        this.updateForm = resp.data.data
      }).catch(err=>{
        console.log(err);
      })
    },
    edit(){
      this.axios.put(`users/${this.userId}`,{
        email:this.updateForm.email,
        mobile:this.updateForm.mobile
      }).then(resp=>{
        if(resp.data.meta.status !== 200) return ElMessage.error('修改失败');
        ElMessage.success('修改成功');
        this.editUserVisible = false;
        this.getAllUser();
      }).catch(err=>{
        console.log(err);
      })
    },
    //删除用户
    delDialog(row){
      this.userId = row.id;
      this.userName = row.username;
      this.delUserVisible = true;

    },
    del(){
      this.axios.delete(`users/${this.userId}`).then(resp=>{
        if(resp.data.meta.status !== 200) return ElMessage.error('删除失败');
        ElMessage.success('删除成功');
        this.delUserVisible = false;
        this.getAllUser();
      }).catch(err=>{
        console.log(err);
      })
    },
    //分配用户权限
    role(id){
      console.log(id);
    },
    //每一页几条数据
    handleSizeChange(val){
      this.queryInfo.pagesize = val;
      this.getAllUser();
    },
    //当前第几页
    handleCurrentChange(val){
      this.queryInfo.pagenum = val;
      this.getAllUser();
    },
    //添加用户
    addUser(formName){
      this.axios.post('users',this.ruleForm).then(data=>{
        console.log(data);
        if( data.data.meta.status !==201 ) return ElMessage.error('添加失败');
        ElMessage.success('添加成功');
        this.resetForm(formName);
        this.getAllUser();    //重新获取数据，实时刷新
      }).catch(err=>{
        console.log(err);
      });
    },
    //关闭表单所做的事情
    resetForm(formName){
      this.$refs[formName].resetFields();
      this.addUserVisible = false;
    },
    //改变状态
    userStatusChange(row){
      //发送请求
      this.axios.put(`users/${row.id}/state/${row.mg_state}`).then(resp=>{
        console.log(resp);
        ElMessage.success(resp.data.meta.msg);
      }).catch(err=>{
        console.log(err);
      })
    }
  }
}
</script>

<style scoped>

</style>
